//
//  KSMainViewController.swift
//  大深圳微博
//
//  Created by kissOnChava on 2016/6/20.
//  Copyright © 2016年 kissOnChava. All rights reserved.
//

import UIKit

class KSMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
        /*
        // 视图控制器
        let homeVC = KSHomeTableViewController()                // 创建 HomeViewController
        /*
         Objective-C
         [类名 类名WithName: ]
         [UIImage imageWithName: ]
         [类名 alloc] initWith: ]
         [UIImage alloc] initWith: ]
         *Swift
         UIImage(named: )
         */
        homeVC.tabBarItem.image = UIImage(named: "tabbar_home") // 设置视图控制器的标签栏项的图片
        homeVC.tabBarItem.title = "首页"                      // 设置视图控制器的标签栏项的标题
        
        // 导航控制器
        //        let nc = UINavigationController(rootViewController: homeVC)      // 创建导航控制器嵌套跟根控制器 homeVC
        //        tc.addChildViewController(nc)                                    // 设置标签控制器的子视图控制器 homeVC
        addChildViewController(UINavigationController(rootViewController: homeVC)) // 设置标签控制器的子视图控制器, 为一个嵌套导航控制器的视图控制器(即为一个包装视图控制器的导航控制器)
        
        
        // 视图控制器
        let messageVC = KSMessageTableViewController()                                   // 创建 messageVC
        messageVC.tabBarItem.image = UIImage(named: "tabbar_message_center")             // 设置视图控制器的标签栏项的图片
        messageVC.title = "消息"                                                          // 设置视图控制器的标题
        addChildViewController(UINavigationController(rootViewController: messageVC)) // 设置标签控制器的子视图控制器, 为一个嵌套导航控制器的视图控制器(即为一个包装视图控制器的导航控制器)
        
        // 视图控制器
        let discoveryVC = KSDiscoveryTableViewController()                                 // 创建 discoveryVC
        discoveryVC.tabBarItem.image = UIImage(named: "tabbar_discover")                   // 设置视图控制器的标签栏项的图片
        discoveryVC.title = "discovery"                                                    // 设置视图控制器的标题
        addChildViewController(UINavigationController(rootViewController: discoveryVC)) // 设置标签控制器的子视图控制器, 为一个嵌套导航控制器的视图控制器(即为一个包装视图控制器的导航控制器)
        
        // 视图控制器
        let profileVC = KSProfileTableViewController()                                   // 创建 profileVC
        profileVC.tabBarItem.image = UIImage(named: "tabbar_profile")                    // 设置视图控制器的标签栏项的图片
        profileVC.title = "我的"                                                          // 设置视图控制器的标题
        addChildViewController(UINavigationController(rootViewController: profileVC)) // 设置标签控制器的子视图控制器, 为一个嵌套导航控制器的视图控制器(即为一个包装视图控制器的导航控制器)
        
        //        profileVC.tabBarItem.image = UIImage(named: #imageLiteral(resourceName: "tabbar_discover"))
        //
        //        profileVC.tabBarItem.image = UIImage(named: "#imageLiteral(resourceName: "tabbar_discover")")
        //
        //        profileVC.tabBarItem.image = UIImage(named: String(#imageLiteral(resourceName: "tabbar_discover")))
        */
        
//        let homeVC = KSHomeTableViewController()
//        setupChildViewController(childController: homeVC, imageName: "tabbar_home", title: "首页")
//        
//        let messageVC = KSMessageTableViewController()
//        setupChildViewController(childController: messageVC, imageName: "tabbar_message_center", title: "    Message")
//        
//        let discoveryVC = KSDiscoveryTableViewController()
//        setupChildViewController(childController: discoveryVC, imageName: "tabbar_discover", title: "发现")
//        
//        let profileVC = KSProfileTableViewController()
//        setupChildViewController(childController: profileVC, imageName: "tabbar_profile", title: "我的")

        // 在 viewDidLoad 中只暴露加载多子视图控制器的方法
        
        
    }
    
    // MARK:  SETUP ChildViewControllers & SETUP ChildViewController
    private func setupChildViewControllers() {
        
        // HOME
        let homeVC = KSHomeTableViewController()
        setupChildViewController(childController: homeVC, imageName: "tabbar_home", title: "HOME")
        
        // MESSAGE
        let messageVC = KSMessageTableViewController()
        setupChildViewController(childController: messageVC, imageName: "tabbar_message_center", title: "MessageDiscovery")
        
        // DISCOVERY
        let discoveryVC = KSDiscoveryTableViewController()
        setupChildViewController(childController: discoveryVC, imageName: "tabbar_discover", title: "发现")
        
        // PROFILE
        let profileVC = KSProfileTableViewController()
        setupChildViewController(childController: profileVC, imageName: "tabbar_profile", title: "我")
        
    }
    
    private func setupChildViewController(childController: UIViewController, imageName: String, title: String) {
        
        // 通过传入的子控制器创建视图控制器 UIViewController 类型的 childController 赋值给 vc
        let vc = childController
        
        // 设置视图控制器的标签栏项的图片
        vc.tabBarItem.image = UIImage(named: imageName)
        
        // 设置视图控制器的标题
        vc.title = title
        
        // 当前标签控制器类添加子视图控制器为附加根视图控制器的导航控制器(导航控制器附加的根视图控制器为视图控制器)
        addChildViewController(UINavigationController(rootViewController: vc))
        
        
//        // 创建附加根视图控制器的导航控制器
//        let nc = UINavigationController(rootViewController: vc)
//
//        // 给当前的类(当前标签控制器类)添加子视图控制器
//        addChildViewController(nc)
        
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
